package com.sinosoft.jwjw.component.controller.system;

import com.sinosoft.jwjw.component.mapper.system.UtilMapper;
import com.sinosoft.jwjw.utils.ConvertUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: 刘冰
 * @date: 2019/2/14
 * @description:
 */
@Controller
@RequestMapping(value = "/util")
public class UtilController {

    @Autowired
    UtilMapper utilMapper;

    @ResponseBody
    @RequestMapping(value = "/")
    public String index(String table) {

        StringBuilder sb_final = new StringBuilder();


        StringBuilder sb_xml = new StringBuilder();
        StringBuilder sb_xml_getListPage = new StringBuilder();
        StringBuilder sb_xml_getListPage_where = new StringBuilder();
        StringBuilder sb_xml_updateById = new StringBuilder();
        StringBuilder sb_xml_deleteById = new StringBuilder();

        String newLine = System.getProperty("line.separator");


        sb_xml.append("<!--               ===============xml生成部分===============                 -->" + newLine);


        sb_xml_getListPage.append("<!--" + newLine);
        sb_xml_getListPage.append("查询分页列表" + newLine);
        sb_xml_getListPage.append("@chineseName 参数说明" + newLine);
        sb_xml_getListPage.append("-->" + newLine);
        sb_xml_getListPage.append("<select id=\"getListPage\" resultType=\"net.sf.json.JSONObject\" parameterType=\"Map\">" + newLine);
        sb_xml_getListPage.append("select " + newLine);


        sb_xml_getListPage_where.append("where a.is_del = 0" + newLine);


        sb_xml_updateById.append("<!--" + newLine);
        sb_xml_updateById.append("修改信息" + newLine);
        sb_xml_updateById.append("-->" + newLine);
        sb_xml_updateById.append("<update id=\"updateById\" parameterType=\"Map\">" + newLine);
        sb_xml_updateById.append("update "+ table + newLine);
        sb_xml_updateById.append("set " + newLine);


        sb_xml_deleteById.append("<!--" + newLine);
        sb_xml_deleteById.append("逻辑删除" + newLine);
        sb_xml_deleteById.append("-->" + newLine);
        sb_xml_deleteById.append("<update id=\"deleteById\" parameterType=\"Map\">" + newLine);
        sb_xml_deleteById.append("update "+ table + "set is_del = 1" + newLine);
        sb_xml_deleteById.append("where id = #{id,jdbcType=VARCHAR}" + newLine);
        sb_xml_deleteById.append("</update>" + newLine);

        String sql = "select column_name,data_type,column_type\n" +
                "from information_schema.columns \n" +
                "where table_name='"+table+"' " +
                "order by ORDINAL_POSITION";
        JSONArray ja = utilMapper.selectAnySQL(sql);
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jo = (JSONObject)ja.get(i);
            String columnName = ConvertUtil.toString(jo.get("column_name")).toLowerCase();
            String dataType = ConvertUtil.toString(jo.get("data_type")).toLowerCase();

            String ocolumnName = getOColumnName(columnName);

            //表前缀默认是a

            //getListPage
            if(i == ja.size()-1){
                sb_xml_getListPage.append("a."+columnName+" " + ocolumnName + "");
            }else{
                sb_xml_getListPage.append("a."+columnName+" " + ocolumnName + ",");
            }
            if(i>0 && i%3 == 0){
                sb_xml_getListPage.append(newLine);
            }

            sb_xml_getListPage_where.append("<if test=\""+ocolumnName+" !=null and "+ocolumnName+" !=''\">" + newLine);
            sb_xml_getListPage_where.append("AND a."+columnName+" like CONCAT('%', #{"+ocolumnName+",jdbcType=VARCHAR}, '%')" + newLine);
            sb_xml_getListPage_where.append("</if>" + newLine);
            if(i == ja.size()-1){
                sb_xml_getListPage_where.append("order by u.id" + newLine);
            }


            //updateById
            sb_xml_updateById.append("<if test=\""+ocolumnName+" !=null and "+ocolumnName+" !=''\">" + newLine);
            sb_xml_updateById.append(""+columnName+" = #{"+ocolumnName+",jdbcType=VARCHAR}," + newLine);
            sb_xml_updateById.append("</if>" + newLine);

        }
        sb_xml_updateById.append("edit_time = #{editTime,jdbcType=TIMESTAMP}," + newLine);
        sb_xml_updateById.append("editor = #{editor,jdbcType=VARCHAR}" + newLine);
        sb_xml_updateById.append("where id = #{id,jdbcType=VARCHAR}" + newLine);
        sb_xml_updateById.append("</update>" + newLine);


        sb_xml_getListPage.append(sb_xml_getListPage_where.toString());
        sb_xml_getListPage.append("</select>" + newLine);


        sb_xml.append(sb_xml_getListPage.toString());
        sb_xml.append(sb_xml_updateById.toString());
        sb_xml.append(sb_xml_deleteById.toString());



        sb_final.append(sb_xml.toString());

        return "<textarea style=\"margin: 0px; height: 100%; width: 100%;\">"+sb_final.toString()+"</textarea>";
    }



    @ResponseBody
    @RequestMapping(value = "/getColumn")
    public String getColumn(String table) {
        String sql = "select column_name,data_type,column_type\n" +
                "from information_schema.columns \n" +
                "where table_name='"+table+"' " +
                "order by ORDINAL_POSITION";
        JSONArray ja = utilMapper.selectAnySQL(sql);
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jo = (JSONObject)ja.get(i);
            String column_name = ConvertUtil.toString(jo.get("column_name"));
            jo.put("columnName",getOColumnName(column_name));
        }
        return ja.toString();
    }
    @ResponseBody
    @RequestMapping(value = "/getTable")
    public String getTable(String database) {
        String sql = "select table_name from information_schema.tables where table_schema='"+database+"'" ;
        JSONArray ja = utilMapper.selectAnySQL(sql);
        return ja.toString();
    }


    public String getOColumnName(String columnName) {

        String ocolumnName = "";
        String[] columnNames = columnName.toLowerCase().split("_");
        for (int i = 0; i < columnNames.length; i++) {
            String temp = columnNames[i];
            if(i == 0){
                ocolumnName += temp;
            }
            else{
                if(temp.length() > 1){
                    ocolumnName += temp.substring(0,1).toUpperCase() + temp.substring(1,temp.length());
                }
                else{
                    ocolumnName += temp.substring(0,1).toUpperCase();
                }
            }
        }

        return ocolumnName;
    }


}
